﻿namespace GreenIsomer.Data.Entities.Ddl {
	using System;
	using System.Xml.Serialization;
	using System.IO;

	[Serializable, XmlRoot("Settings", Namespace = Namespace)]
	[XmlInclude(typeof(CreateScriptGenerator))]
	[XmlInclude(typeof(DropScriptGenerator))]
	public class Settings {

		public static Settings Load(string path) {
			XmlSerializer serializer = new XmlSerializer(typeof(Settings));
			using(TextReader reader = new StreamReader(path))
				return (Settings)serializer.Deserialize(reader);
		}

		public const string Namespace = "http://greenisomer.com/schema/data/entities/ddl/generate/1.0/";

		[NonSerialized]
		private ScriptGeneratorCollection generators;

		[NonSerialized]
		private ProviderSettingsCollection providerSettings;

		public Settings() {
			generators = new ScriptGeneratorCollection();
			ExtendedDefinition = new ExtendedDefinition();
			providerSettings = new ProviderSettingsCollection();
		}

		[XmlElement("ScriptGenerator")]
		public ScriptGeneratorCollection ScriptGenerators {
			get { return generators; }
		}

		[XmlElement("ExtendedDefinition")]
		public ExtendedDefinition ExtendedDefinition { get; set; }

		[XmlElement("ProviderSettings")]
		public ProviderSettingsCollection ProviderSettings {
			get { return providerSettings; }
		}

		public void Save(string path) {
			XmlSerializer serializer = new XmlSerializer(typeof(Settings));
			using(TextWriter writer = new StreamWriter(path))
				serializer.Serialize(writer, this);
		}

	}

}